package com.sgb.kjwl.view.ui.adapter.shopping;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.MyMediaUtils;

import java.util.List;

import cn.jzvd.JzvdStd;


/**
 * 作者:张磊
 * 日期:2018/6/20
 */
public class NormalRecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder> {


    private final boolean mIsVideos; // 是否是视频集

    public NormalRecyclerAdapter(List<String> beans, boolean isVideos) {
        super(R.layout.adapter_data, beans);
        mIsVideos = isVideos;
    }

    @Override
    protected void convert(BaseViewHolder helper, final String item) {

        String key = "视频";
        helper
                .setVisible(R.id.fl, item.contains(key))
                .setVisible(R.id.ll_bofang, !mIsVideos)
                .setVisible(R.id.iv_img, !item.contains(key));
        JzvdStd jz_video = helper.getView(R.id.jz_video);
        jz_video.videos = mIsVideos;
        jz_video.startButton.setVisibility(mIsVideos ? View.VISIBLE : View.GONE);
        jz_video.setOnClick((state) ->
                {
                    if (mIsVideos) return;
                    UiUtil.sendMsg(EventConstant.EVENT_STATE, state);
                    helper.setVisible(R.id.ll_bofang, state == 7 || state == 0);
                }
        );
        helper.getView(R.id.ll_bofang).setOnClickListener(v -> {
            helper.setVisible(R.id.ll_bofang, false);
            jz_video.startVideo();
        });

        ImageView iv_img = helper.getView(R.id.iv_img);
        if (item.contains(key)) {
            String[] str = item.split(key);
            jz_video.setUp(str[0], "");
            if (TextUtils.isEmpty(str[1]) || ("null".equals(str[1]))) {
                jz_video.posterImageView.setImageBitmap(MyMediaUtils.setVideoFirstImg(str[0], mContext));
            } else {
                Glide.with(mContext)
                        .load(str[1])
                        .into(jz_video.posterImageView);
            }
        } else {
            Glide.with(mContext).load(item).into(iv_img);
        }
    }

}
